<?php
namespace Home\Controller;

class ProductController extends \Home\Controller\BaseController
{

	//获取商品信息
	public function goodlist(){
		$param = I('params');
		$type = $param['type'];
		$area = $param['area'];
		$page = $param['page'];
		$size = $param['size'];

		$res = M("Good")
			->where(array("type"=>$type,"area"=>$area))
			->field(array('title','sub_title','range','status','create_time','type','purchase_num','img_path','production_time','area','original_price','discount_price','stock','appointment',"end_time","id"))
			->page($page, $size)
			->select();
		$return = array();
		foreach($res as $key=>$val){
			$return[$key] = $val;
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['stock'] = intval($val['stock']);
			$return[$key]['end_time'] = strtotime($val['end_time']);
			$return[$key]['img_path'] = explode(",",$val['img_path'])[0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//获取商品详情
	public function goodinfo(){
		$param = I("params");
		$id = $param['id'];
		//类别
		$return['categorylist'] = M("GoodCategory")->where(array("good_id"=>$id))->field(array("id","name","value"))->select();
		//所有商品下属的价格
		$pricelist = M("GoodPrice")->where(array("good_id"=>$id))->field(array("id","category_id","original_price","discount_price","stock","code"))->select();
		//商品的详情
		$return['goodinfo'] = M("Good")->where(array("id"=>$id))->find();
		foreach($pricelist as $key=>$val){
			$return['pricelist'][$key]=$val;
			$return['pricelist'][$key]["original_price"] = floatval($val['original_price']);
			$return['pricelist'][$key]["discount_price"] = floatval($val['discount_price']);
			$return['pricelist'][$key]["category_id"] = explode(",",$val['category_id']);
		}
		$return['goodinfo']['stock'] = intval($return['goodinfo']['stock']);
		$return['goodinfo']['end_time'] = strtotime($return['goodinfo']['end_time']);
		$return['goodinfo']['min_price'] = floatval($return['goodinfo']['min_price']);
		$return['goodinfo']['img_path'] = explode(",",$val['img_path']);
		$return['goodinfo']['total_count'] = intval("1028");
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}

}